package com.sh.rabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/send")
public class SendMsgController {

    public static final String QUEUE = "confirm.queue";

    public static final String EXCHANGE = "confirm.exchange";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/{msg}")
    public void sendMsg(@PathVariable("msg") String msg) {
        rabbitTemplate.convertAndSend("X", "XA", "消息是10秒的" + msg);
        rabbitTemplate.convertAndSend("X", "XB", "消息是30秒的" + msg);
        log.info("当前时间{}，发送消息成功，{}", new Date(), msg);
    }

    @GetMapping("/confirm/{msg}")
    public void sendCnfirmMsg(@PathVariable("msg") String msg) {
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(EXCHANGE+1, "key1",  msg, correlationData);
        log.info("当前时间{}，发送消息成功，{}", new Date(), msg);
    }

    @GetMapping("/confirm1/{msg}")
    public void sendCnfirm1Msg(@PathVariable("msg") String msg) {
        CorrelationData correlationData = new CorrelationData("2");
        rabbitTemplate.convertAndSend(EXCHANGE, "key1",  msg, correlationData);
        log.info("当前时间{}，发送消息成功，{}", new Date(), msg);
    }


}
